package com.wiz.weixin.controllor;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wiz.weixin.manager.TencentManager;
import com.wiz.weixin.service.PrPriceService;
import com.wiz.weixin.service.StationService;
import com.wiz.weixin.util.PageView;
/**
 * 当前油价
 * @author xxy
 *
 */
@Controller
@RequestMapping("/prPrice")
public class PrPriceControllor {
	private transient final Logger log=LoggerFactory.getLogger(getClass());
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	@Resource
	private PrPriceService prPriceService;
	@Resource
	private StationService stationService;
	@Resource
	private TencentManager tencentManager;

	@RequestMapping(value="/list")
	public String list(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) {
		String code = request.getParameter("code");
		try {
			String openid =tencentManager.getOpenId(code);
			session.setAttribute("openid", openid);
			//查询客户的所有会员
			List<String>hqIds = stationService.getHQIds(openid);
			session.setAttribute("hqIds", hqIds);
			
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return "/prPrice/price";
		
	}
	@RequestMapping(value="/prices")
	@ResponseBody
	public String prices(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "pageNo", required = false,defaultValue="1") int pageNo,
			@RequestParam(value = "pageSize", required = false,defaultValue="10") int pageSize,
			HttpSession session, Model model) {
		PageView pageView = new PageView(pageSize, pageNo);
		try {
			//查询客户的所有会员
			List<String> hqIds =  (List<String>) session.getAttribute("hqIds");
			
			pageView = prPriceService.listPrPrice(pageView,hqIds);
			pageView.setCode(200);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return gson.toJson(pageView);
		
	}
}
